package com.ricohgag.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ricohgag.entity.Goods;
import com.ricohgag.entity.UserCartLog;
import com.ricohgag.entity.custom.UserCartLogVo;
import com.ricohgag.mapper.UserCartLogMapper;
import com.ricohgag.service.GoodsService;
import com.ricohgag.service.UserCartLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 肖晖
 * @since 2018-08-25
 */
@Service
public class UserCartLogServiceImpl extends ServiceImpl<UserCartLogMapper, UserCartLog> implements UserCartLogService {
    @Autowired
    private UserCartLogMapper userCartLogMapper;
    @Autowired
    private GoodsService goodsService;

    /**
     * 查询用户购物车详情，使用了包装类UserCartLogVo
     * @param userId
     * @return
     * @throws Exception
     */
    @Override
    public List<UserCartLogVo> selectCartVoByUserId(Integer userId) throws Exception{
        List<UserCartLog> userCartLogs = userCartLogMapper.selectList(new QueryWrapper<UserCartLog>().eq("user_id", userId));
        System.err.println(userCartLogs);
        List<UserCartLogVo> userCartLogVos = new ArrayList<>();

        for(UserCartLog userCartLog:userCartLogs){
            BigDecimal totalPrice = new BigDecimal(0);
            UserCartLogVo userCartLogVo = new UserCartLogVo();
            Goods goods = goodsService.selectGoodsOne(userCartLog.getGoodsId());
            System.out.println("goods: "+goods);
            userCartLogVo.setUserCartLog(userCartLog);
            userCartLogVo.setGoods(goods);
            totalPrice = goods.getSellPrice().multiply(new BigDecimal(userCartLog.getGoNum()));
            System.err.println("totalPrice: "+totalPrice);
            userCartLogVo.setTotalPrice(totalPrice);
            userCartLogVos.add(userCartLogVo);
        }

        System.out.println("userCartLogs: "+userCartLogVos);
        return userCartLogVos;
    }

    /**
     * 根据商品id和商品数量以及用户id更改购物车的商品数量
     * @param id
     * @return
     */
    @Override
    public boolean updateByGoodsId(Integer id, Integer userId, Integer num){
        QueryWrapper<UserCartLog> qw = new QueryWrapper<>();
        qw.eq("goods_id", id).eq("user_id", userId);
        UserCartLog userCartLog = userCartLogMapper.selectList(qw).get(0);

        //更改数量
        userCartLog.setGoNum(num);
        int count = userCartLogMapper.updateById(userCartLog);

        return count>0;
    }


    @Override
    public int update(UserCartLog userCartLog) throws Exception{
        int count = userCartLogMapper.updateById(userCartLog);
        return count;
    }

    @Override
    public int delete(Integer id)throws Exception{
        int count = userCartLogMapper.deleteById(id);
        return count;
    }

    @Override
    public int deleteByUserId(Integer userId)throws Exception{
        int count = userCartLogMapper.delete(new UpdateWrapper<UserCartLog>()
                .eq("user_id", userId));
        return count;
    }

    @Override
    public int insertCartLog(UserCartLog userCartLog) throws Exception{
        int count = userCartLogMapper.insert(userCartLog);
        return count;
    }
}
